package com.jubao.ejb.dao.impl;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.jubao.ejb.dao.BrandDaoLocal;
import com.jubao.ejb.dao.BrandDaoRemote;
import com.jubao.ejb.dao.interceptor.TracingInterceptor;
import com.jubao.ejb.dao.util.DaoConstant;
import com.jubao.ejb.entity.Brand;

/**
 * @author conan
 * 
 */
@Stateless
@Local( { BrandDaoLocal.class })
@LocalBinding(jndiBinding = DaoConstant.BrandDaoLocalJndiName)
@Remote( { BrandDaoRemote.class })
@RemoteBinding(jndiBinding = DaoConstant.BrandDaoRemoteJndiName)
@Interceptors( { TracingInterceptor.class })
public class BrandDaoImpl implements BrandDaoLocal, BrandDaoRemote {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7862653994239516356L;

	static Logger logger = Logger.getLogger(BrandDaoImpl.class);

	@PersistenceContext(unitName = "default")
	protected EntityManager em;

	public Brand find(String brandId) {
		Query q = em.createNamedQuery("BrandDao.eql.findBrandById").setParameter("brandId", brandId).setMaxResults(1)
				.setHint("org.hibernate.cacheable", new Boolean(true));
		//.setHint("org.hibernate.cacheRegion", "/entity/Category/Single")
		List<Brand> brands = q.getResultList();
		Brand result = null;
		if (brands != null && brands.size() > 0) {
			result = brands.get(0);
		}
		return result;
	}

	public Brand find(long brandSysId) {
		return em.find(Brand.class, brandSysId);
	}

	public Brand save(Brand brand) {
		return em.merge(brand);
	}

}
